from api import *
import pygame
import os
from map import get_map

# import random

GO = False


# noinspection PyProtectedMember
class goEntity(Entity):
    def __init__(self, pos: tuple[int, int], texture: str, type: int, player: Entity, ticks: int | float = 0):
        super(goEntity, self).__init__(pos, texture, type, ticks)
        self.player = player

    def ai(self):
        if GO:
            x = y = 0
            if self.x > self.player.x:
                x -= 2
            elif self.x < self.player.x:
                x += 2
            if self.y > self.player.y:
                y -= 2
            elif self.y < self.player.y:
                y += 2
            # x += random.randint(-16, 16)
            # y += random.randint(-16, 16)
            self.x += x
            self.y += y
            self.pos = self.x, self.y
            self.hitbox._move(x, y)
            if self.hitbox.pz(self.player.hitbox):
                print("你被抓住了!")
                os._exit(0)


class Player(Entity):
    def go(self, event):
        if self.type == 1:
            try:
                player_x, player_y = \
                    {pygame.K_LEFT: (-16, 0), pygame.K_RIGHT: (16, 0),
                     pygame.K_UP: (0, -16), pygame.K_DOWN: (0, 16)}[event.key]
            except KeyError:
                return
            else:
                global GO
                GO = True
            # noinspection PyProtectedMember
            self._move(player_x, player_y)
            player_x = player_y = 0


"""map = [
    "#*############################",
    "#*#*##**#***#*******#***#*##*#",
    "#***#**##*#*#*#####*#*#*#****#",
    "#*###*#######*****#*###*#*##*#",
    "#*****#*****#####*#***######*#",
    "##*##***#*#*#*****#*#*#***#**#",
    "#***#####*#*#####*#*#*###*##*#",
    "#*#*#***###*******#*#******#*#",
    "#*****#***#########*########*#",
    "#########***#*****#***##*****#",
    "#*#*****###*#*#*#*#*#*#**###*#",
    "#***###***#*#*#####*#***##*#*#",
    "##*########*#*******#####****#",
    "#**#******###########***##*###",
    "####*##*#***#******#######***#",
    "#*#####*#######*##*********###",
    "#*#***********#*##*###########",
    "#*#*###*#*#*#####************#",
    "#***###*****#####*##########*#",
    "############################*#"
]"""
map = get_map()

i = j = 0

for blocks in map:
    for block in blocks:
        if block == "#":
            blocklist.append(Block((j * blocklong, i * blocklong), "images/light_blue_wool.png", 1,
                                   (j * blocklong, i * blocklong), ((j + 1) * blocklong, (i + 1) * blocklong)))
        j += 1
    i += 1
    j = 0

player = Player((blocklong, blocklong), "images/green_wool.png", 1)

entitylist.append(player)

entitylist.append(goEntity((29 * blocklong, blocklong), "images/red_wool.png", 2, player, 10))


# noinspection PyProtectedMember
def f():
    for i in entitylist:
        i: Entity
        if i.type != 1:
            continue
        if i.pos == (29 * blocklong, 19 * blocklong):
            print("你赢了!")
            exit(0)
        elif i.pos == (blocklong, 0):
            pygame.display.set_caption("开始!")
        else:
            pygame.display.set_caption("迷宫")


regFunc(f)

setVar(1, True)
setVar(2, True)
setVar(3, True)
